package com.gl.emms.whale.db;

import java.util.ArrayList;
import java.util.List;

import com.gl.emms.whale.util.ApplacationInfo;
import com.gl.emms.whale.util.RechargeRecordEntity;
import com.gl.emms.whale.util.SystemMsgEntity;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class RechargeRecordDB {
	private SQLiteDatabase db;

	public RechargeRecordDB(Context context) {
		db = context.openOrCreateDatabase(ApplacationInfo.DBNAME,
				Context.MODE_PRIVATE, null);
	}

	public void saveRecord(RechargeRecordEntity entity) {
		db.execSQL("CREATE table IF NOT EXISTS Record"
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, bill TEXT, money TEXT, time TEXT, state TEXT, user TEXT);");

		db.execSQL(
				"insert into Record"
						+ " (bill,money,time,state,user) values(?,?,?,?,?)",
						new Object[] { entity.getBill(), entity.getMoney(),entity.getTime(),entity.getState(),entity.getUser()});

	}

	public List<RechargeRecordEntity> getRecord() {
		List<RechargeRecordEntity> list = new ArrayList<RechargeRecordEntity>();
		db.execSQL("CREATE table IF NOT EXISTS Record"
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,bill TEXT, money TEXT,time TEXT,state TEXT,user TEXT);");
		Cursor c = db.rawQuery("SELECT * from Record", null);
		while (c.moveToNext()) {

			int msgId = c.getInt(c.getColumnIndex("_id"));
			String bill = c.getString(c.getColumnIndex("bill"));
			String money = c.getString(c.getColumnIndex("money"));
			String time = c.getString(c.getColumnIndex("time"));
			String state = c.getString(c.getColumnIndex("state"));
			String user = c.getString(c.getColumnIndex("user"));

			RechargeRecordEntity entity = new RechargeRecordEntity(bill, "",money, time,state,user);
			list.add(entity);
		}
		c.close();
		return list;
	}
	public void removeRecord(int id) {
		db.execSQL("CREATE table IF NOT EXISTS Record"
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,bill TEXT, money TEXT,time TEXT,state TEXT,user TEXT);");
		db.execSQL("DELETE from Record where _id = ?",new Object[] { id });
	}
	public int getMaxId() {
		db.execSQL("CREATE table IF NOT EXISTS Record"
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,bill TEXT, money TEXT,time TEXT,state TEXT,user TEXT);");
		String strSql = " Select  seq From sqlite_sequence  Where name = 'Record'";

		Cursor mCursor = db.rawQuery(strSql, null);
		int maxId = 0;
		if(mCursor.moveToNext())
		{
			maxId = mCursor.getInt(mCursor.getColumnIndex("seq")); 
		}
		return maxId;
	}

	public void close() {
		if (db != null)
			db.close();
	}
}
